<?php
declare(strict_types=1);
/**
 * 发送短信基类
 */
namespace Service\VenderClient\Sms;
use Service\Code\Normal;
use Service\BaseService;
use Service\ExtraService\TaskWorker;
use Service\ExtraService\Template;
class SmsWwtlClient extends BaseService
{
    protected $config;
    public function __construct( array $apiBody = [] )
    {
        $this->config = $apiBody;
    }
    
    /**
     * 发送短信
     * @param string $mobile
     * @param string $message
     */
    public function send(string $mobile ,string $msg,string $temp = 'default')
    {
        $temp_contents = Template::$temp[$temp];
        $msg = str_replace('{code}',$msg,$temp_contents);
        $message = $msg.$this->config['signature'];
        $url = "http://cf.51welink.com/submitdata/Service.asmx/g_Submit";
        $data= [
            'sname'   => $this->config['sname'],
            'spwd'    => $this->config['spwd'],
            'scorpid' => '',
            'sprdid'  => $this->config['sprdid'],
            'sdst'    => $mobile,
            'smsg'    => $message,
        ];
        $parameter = [
            [
                'tag'       => 'sms_wwtl',
                'url'       => $url,
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'x-www-form-urlencoded',
                'header'    => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        $response   = $resTask['response']['result'];
        $res = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if ( $res['State'] == 0 ) {
            return Normal::returnCode(Normal::SUC,"发送成功");
        }
        return Normal::returnCode(Normal::FAIL,'发送失败:'.$res['MsgState']);
    }
}
